<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

//设置cookieId，功能与sessionid一样

function getCookieId()
{
    $request = think\Request::instance();
    $domain = $request->domain();
    $domain_parts=explode(':',$domain);

    $domainNoHttp=[];
    foreach ($domain_parts as $k=>$v){
        if($k!=0){
            $domainNoHttp[]=$v;
        }
    }
    $domainNoHttp=implode(':',$domainNoHttp);
    $length=strlen($domainNoHttp);

    $domainNoHttp= substr( $domainNoHttp,2,$length-2);

     $domain=$domainNoHttp;

     $cookieIdName='phpcookieid_'.md5($domain);
     $cookieIdValue=cookie($cookieIdName);
     if(empty($cookieIdValue))
     {
         $cookieIdValue=md5($domain.uniqid().rand(100000,999999));
         cookie($cookieIdName,$cookieIdValue);
     }

     return $cookieIdValue;
}
function result($code,$msg='',$data='',$success=false)
{
    if($code==200){
        $success=true;
    }
    return json(['code'=>$code,'msg'=>$msg,'data'=>$data,'success'=>$success]);
}

/**
 * 连续创建目录
 *
 * @param string $dir 目录字符串
 * @param int $mode 权限数字
 * @return boolean
 */
function makeDir($dir, $mode = "0777") {
//    if (!dir) return false;
    if(!file_exists($dir)) {
        return mkdir($dir,$mode,true);
    } else {
        return true;
    }

}